<!DOCTYPE html>
<html>
<head>
    <title>评论区</title>
    <link rel="stylesheet" href="${request.contextPath}/resources/layui/css/layui.css">
</head>
<body>
<div style="margin-left: 10px;margin-top: 10px">
    <form class="layui-form layui-form-pane">
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">评论人</label>
                <div class="layui-input-inline" style="width: 150px">
                    <input type="text" class="layui-input" id="userName" placeholder="评论人" autocomplete="off">
                </div>
            </div>
            <div class="layui-inline">
                <button type="button" class="layui-btn" id="searchBtn">查询</button>
            </div>
        </div>
    </form>
    <hr>
    <table id="dataTable" lay-filter="dataTableFilter"></table>
</div>

<script type="text/html" id="headerBtns">
    <div class="layui-btn-group">
        <%--        <shiro:hasPermission name="car:add">--%>
        <button class="layui-btn layui-btn-sm" type="button" lay-event="add">
            <i class="layui-icon layui-icon-add-1"></i>添加
        </button>
        <%--        </shiro:hasPermission>--%>
    </div>
</script>

<script type="text/html" id="addTpl">
    <form class="layui-form layui-form-pane" style="margin-top: 15px;margin-left: 15px">
        <div class="layui-form-item">
            <label class="layui-form-label">评论人</label>
            <div class="layui-input-inline">
                <input type="text" class="layui-input" name="userName" placeholder="评论人" lay-verify="required" lay-reqText="评论人不能为空" />
            </div>
        </div>
        <div class="layui-form-item layui-form-text" style="width: 300px">
            <label class="layui-form-label">评论</label>
            <div class="layui-input-block">
                <textarea class="layui-textarea" name="mean" placeholder="评论"></textarea>
            </div>
        </div>
        <button id="subBtn" type="button" lay-filter="subBtnFilter" lay-submit style="display: none"></button>
    </form>
</script>

<script type="text/html" id="rowBtns">
    <div class="layui-btn-group">
        <button class="layui-btn layui-btn-sm layui-btn-danger" type="button" lay-event="delete">
            <i class="layui-icon layui-icon-delete"></i>删除
        </button>
    </div>
</script>

<script src="${request.contextPath}/resources/layui/layui.js"></script>
<script>
    layui.use(['form', 'table', 'jquery', 'layer','upload','laydate'], function () {
        let cxt = "${request.contextPath}";
        let form = layui.form;
        let table = layui.table;
        let $ = layui.jquery;
        let layer = layui.layer;
        let upload = layui.upload;
        let laydate = layui.laydate;

        let tabOpt = {
            elem: "#dataTable",
            id: "dataTableId",
            url: cxt + "/comment/page.do",
            toolbar:"#headerBtns",
            cols: [[
                {field: "commentId", title: "评论ID",minWidth:50},
                {field: "mean", title: "评论"},
                {field: "userName", title: "评论人",minWidth:50},
                {field: "createTime", title: "创建时间",minWidth:50},
                {title: "操作",toolbar:"#rowBtns",minWidth:190,fixed:"right"}
            ]],
            page: true,
            parseData: function (rs) {
                if (rs.code == 200) {
                    return {
                        code: rs.code,
                        msg: rs.msg,
                        count: rs.data.total,
                        data: rs.data.records
                    }
                }
            },
            response: {
                statusCode: 200
            }
        };
        //渲染数据表格
        let tabIns = table.render(tabOpt);
        //搜索
        $("#searchBtn").click(function (){
            let userName = $("#userName").val();

            tabIns.reload({
                where:{
                    username:userName,
                }
            })
        });

        //头工具栏事件
        table.on("toolbar(dataTableFilter)",function (d){
            let event = d.event;
            if(event == "add"){
                add();
            }
        })

        //新增书籍
        function add(){
            let layerOpt = {
                title:"编辑",
                type:1,
                content:$("#addTpl").html(),
                area:['330px','560px'],
                btn:['确认','取消'],
                btnAlign:'c',
                anim:4,
                success:function (layero,index){
                    //渲染表单
                    form.render();
                    //设置表单提交监听事件
                    form.on("submit(subBtnFilter)",function (d){
                        let formData = d.field;
                        //进行ajax提交
                        $.post(cxt+"/comment/add.do",formData,function (rs){
                            layer.msg(rs.msg);
                            if(rs.code != 200){
                                return false;
                            }
                            //刷新表格
                            $("#searchBtn").click();
                            //关闭弹层
                            layer.close(index);
                        });

                        return false;//阻止默认提交行为
                    });
                },
                yes:function (index,layero){
                    $("#subBtn").click();
                }
            };
            layer.open(layerOpt);
        }

        //行工具栏事件
        table.on("tool(dataTableFilter)",function (d){
            let event = d.event;
            let rowData = d.data;
            if (event == "delete"){
                del(rowData);
            }
        })
        /**
         * 删除
         * @param rowData  行数据
         */
        function del(rowData){
            layer.confirm("确定要删除吗?",function (index){
                $.get(cxt+"/comment/delete.do?commentId="+rowData.commentId,function (rs){
                    layer.msg(rs.msg);
                    layer.close(index);//关闭弹出层
                    $("#searchBtn").click();
                })
            })
        }


    })
</script>
</body>
</html>
